<?php
Class ControllerMartCrmCrmcustomer extends Controller{
 public function index() {
		$this->load->language('Mart_Crm/Crm_customer');		
    	        $this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('Mart_Crm/Crm_customer');              
                $this->getList();		
  	}
public function bills(){
   $this->load->language('Mart_Crm/Crm_customer');
   $this->document->setTitle($this->language->get('heading_title'));
   $this->load->model('Mart_Crm/Crm_customer');
   $this->getbills();
}
public function detailedbills(){
    $this->load->language('Mart_Crm/Crm_customer');

   $this->document->setTitle($this->language->get('heading_title'));
   $this->load->model('Mart_Crm/Crm_customer');
   $this->getdetailedbills(); 
}
public function customer(){
    $this->load->language('Mart_Crm/Crm_customer');
    $this->document->setTitle($this->language->get('heading_title'));
    $this->load->model('Mart_Crm/Crm_customer');
    $this->getcustomer();
}

public function prints(){
   $this->load->model('Mart_Crm/Crm_customer');
   $this->printbills();
}

private function getList()
{
               if (isset($this->request->get['last_name'])) {
		$last_name = $this->request->get['last_name'];
		} else {
			$last_name = NULL;
		}   
		 if (isset($this->request->get['day'])) {
		$day = $this->request->get['day'];
		} else {
			$day = NULL;
		}  
                 if (isset($this->request->get['email'])) {
		$email = $this->request->get['email'];
		} else {
			$email = NULL;
		}  
                 if (isset($this->request->get['month'])) {
		$month = $this->request->get['month'];
		} else {
			$month = NULL;
		}   
                 if (isset($this->request->get['year'])) {
		$year = $this->request->get['year'];
		} else {
			$year = NULL;
		}   
		if (isset($this->request->get['first_name'])) {
			$first_name = $this->request->get['first_name'];
		} else {
			$first_name = NULL;
		}
		if (isset($this->request->get['phonemobile'])) {
			$phonemobile = $this->request->get['phonemobile'];
		} else {
			$phonemobile = NULL;
		}
                if (isset($this->request->get['birthday'])) {
			$birthdayy = $this->request->get['birthday'];
                        $birthday  = date('Y-m-d',strtotime($birthdayy));
		} else {
			$birthday = NULL;
		}
                if (isset($this->request->get['point'])) {
			$point = $this->request->get['point'];
		} else {
			$point = NULL;
		}
                  if (isset($this->request->get['district'])) {
			$district = $this->request->get['district'];
		} else {
			$district = NULL;
		}
                  if (isset($this->request->get['city'])) {
			$city = $this->request->get['city'];
		} else {
			$city = NULL;
		}
//sort dung de sap xep cho lenh order by
                if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'first_name';
		}
//order dung de sap xep theo 'ASC' hay 'DSCE'		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
																		
		$url = '';
		
		if (isset($this->request->get['last_name'])) {
			$url .= '&last_name=' . urlencode($this->request->get['last_name']);
		}
		  if (isset($this->request->get['day'])) {
			$url .= '&day=' . $this->request->get['day'];
		}
                  if (isset($this->request->get['month'])) {
			$url .= '&month=' . $this->request->get['month'];
		}
                  if (isset($this->request->get['year'])) {
			$url .= '&year=' . $this->request->get['year'];
		}
		if (isset($this->request->get['first_name'])) {
			$url .= '&first_name=' . $this->request->get['first_name'];
		}
		
                if (isset($this->request->get['phonemobile'])) {
			$url .= '&phonemobile=' . $this->request->get['phonemobile'];
		}
                if (isset($this->request->get['birthday'])) {
			$url .= '&birthday=' . $this->request->get['birthday'];
		}
                 if (isset($this->request->get['district'])) {
			$url .= '&district=' . $this->request->get['district'];
		}
                 if (isset($this->request->get['city'])) {
			$url .= '&city=' . $this->request->get['city'];
		}
                if (isset($this->request->get['point'])) {
			$url .= '&point=' . $this->request->get['point'];
		}
                if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
                if (isset($this->request->get['email'])) {
			$url .= '&email=' . $this->request->get['email'];
		}
						
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       	        'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token']),
       		'text'      => $this->language->get('text_home'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('Mart_Crm/Crm_customer', 'token=' . $this->session->data['token'] . $url),
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
		
		//echo $birthday;		
		$data = array(	
                        'first_name'	  => $first_name, 
			'last_name'	  => $last_name,
			'phonemobile'	  => $phonemobile,
			'point'           => $point,
                        'day'       => $day,
                        'month'     => $month,
                        'year'      => $year,
                        'district'        => $district,
                        'city'            => $city,
                        'sort'            => $sort,
			'order'           => $order,
			'start'           => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'           => $this->config->get('config_admin_limit'),
                    'email'=>$email
		);			
    		
		$results = $this->model_Mart_Crm_Crm_customer->getcustomer($data);
            
                $customer_total = $this->model_Mart_Crm_Crm_customer->getCustomertotal();
              
                if($customer_total!=null)
                {
                if($results!=null)
                {
                foreach ($results as $result) {
			$bills = array();
			
			$bills[] = array(
				'href' => $this->url->link('Mart_Crm/Crm_customer/bills', 'token=' . $this->session->data['token'] . '&id=' . $result['id'] , 'SSL')                               
			);
                        $infor_customer = $this->url->link('Mart_Crm/Crm_customer/customer','token=' . $this->session->data['token'].'&id_customer=' . $result['id'],'SSL');
                        $a = $result['birthday'];
                         $format = date('d-m-Y',strtotime($a));
			$this->data['customers'][] = array(
				'last_name' => $result['last_name'],
				'first_name'       => $result['first_name'],
				'phone_mobile' => $result['phone_mobile'],
                                'birthday'     => $format,
				'points' => $result['points'],	
                                'district' => $result['primary_address_street'],
                                'city'    => $result['primary_address_city'],
				'bills'     => $bills,                                
                                'href1'  => $infor_customer,
                            'email'=>$result['email_address']
			);
		}
                }
                else {
                    $this->data['customers'] = NULL;
                }
                }
                else
                {
                     $this->data['customers'] = NULL;
                }
 		$this->data['heading_title'] = $this->language->get('heading_title');
		 
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['column_district']  = $this->language->get('column_district');
                $this->data['column_city']   = $this->language->get('column_city');
                $this->data['column_email']   = $this->language->get('column_email');
		$this->data['column_firstname'] = $this->language->get('column_firstname');
		$this->data['column_lastname'] = $this->language->get('column_lastname');				
		$this->data['column_phonemobile'] = $this->language->get('column_phonemobile');
                $this->data['column_birthday'] = $this->language->get('column_birthday');
                $this->data['column_point']       = $this->language->get('column_point');
                $this->data['column_bills']      = $this->language->get('column_bills');  	
                $this->data['column_customer']   = $this->language->get('column_customer');
                $this->data['button_export_excel'] = $this->language->get('button_export_excel');
                $this->data['token'] = $this->session->data['token'];
                
//                $url = '';
//
//		if (isset($this->request->get['first_name'])) {
//			$url .= '&first_name=' . urlencode(html_entity_decode($this->request->get['first_name'], ENT_QUOTES, 'UTF-8'));
//		}
//		  if (isset($this->request->get['day'])) {
//			$url .= '&day=' . $this->request->get['day'];
//		}
//                  if (isset($this->request->get['month'])) {
//			$url .= '&month=' . $this->request->get['month'];
//		}
//                  if (isset($this->request->get['year'])) {
//			$url .= '&year=' . $this->request->get['year'];
//		}
//		if (isset($this->request->get['last_name'])) {
//			$url .= '&last_name=' . urlencode(html_entity_decode($this->request->get['last_name'], ENT_QUOTES, 'UTF-8'));
//		}
//		
//		if (isset($this->request->get['phonemobile'])) {
//			$url .= '&phonemobile=' . $this->request->get['phonemobile'];
//		}
//		
//		if (isset($this->request->get['point'])) {
//			$url .= '&point=' . $this->request->get['point'];
//		}
//								
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}
//
//		if (isset($this->request->get['page'])) {
//			$url .= '&page=' . $this->request->get['page'];
//		}
                
                $this->data['sort_firstname'] = $this->url->link('Mart_Crm/Crm_customer', 'token=' . $this->session->data['token'] . '&sort=first_name' . $url, 'SSL');
		$this->data['sort_lastname'] = $this->url->link('Mart_Crm/Crm_customer', 'token=' . $this->session->data['token'] . '&sort=last_name' . $url, 'SSL');
		$this->data['sort_phonemobile'] = $this->url->link('Mart_Crm/Crm_customer', 'token=' . $this->session->data['token'] . '&sort=phone_mobile' . $url, 'SSL');
                $this->data['sort_birthday'] = $this->url->link('Mart_Crm/Crm_customer', 'token=' . $this->session->data['token'] . '&sort=birthday' . $url, 'SSL');
		$this->data['sort_point'] = $this->url->link('Mart_Crm/Crm_customer', 'token=' . $this->session->data['token'] . '&sort=points' . $url, 'SSL');
                // Export Excel
                $this->data['export_excel'] = $this->url->link('tool/export/downloadcustomer', 'token=' . $this->session->data['token'].$url,'SSL');
             
                
		$pagination = new Pagination();
		$pagination->total = $customer_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
//khi ta kich su kien chuyen trang(goi den bien nay) thi dong 112 se thuc thi cau lenh tra ve chuoi url nhu ben duoi.khi do ham index trong customercontroller se thuc thi voi chuoi url moi co tham so truyen vao
		$pagination->url = $this->url->link('Mart_Crm/Crm_customer', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

		$this->data['pagination'] = $pagination->render();
                if($birthday)
                {
                $a = date('d-m-Y',  strtotime($birthday));
                }
                else { $a = NULL;}
                $this->data['last_name'] = $last_name;
                $this->data['first_name'] = $first_name;
                $this->data['phonemobile'] = $phonemobile;
                $this->data['birthday'] = $a;
                $this->data['point'] = $point;
                $this->data['day']  = $day;
                $this->data['month'] = $month;
                $this->data['year']  = $year;
                $this->data['district']  = $district;
                $this->data['city']  = $city;
                $this->data['sort'] = $sort;
                $this->data['order'] = $order;
                $this->data['email'] = $email;
                
		$this->template = 'Mart_Crm/Crm_customer.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render());   
}
private function getbills()
{
             if (isset($this->request->get['id'])) {
		$id = $this->request->get['id'];
		} else {
			$id = NULL;
		}  
              if(isset($this->request->get['bill'])){
                $bill =  $this->request->get['bill'];
                }
                 else {
                     $bill = NULL;
                 }
              if(isset($this->request->get['date_bill'])){
                  $date_bills = $this->request->get['date_bill'];
                  $date_bill  = date('Y-m-d',strtotime($date_bills));
                  }
                 else {
                     $date_bill= NULL;
                     $date_bills = NULL;
                 }        
                if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
                $url = '';
		
                if(isset($this->request->get['bill']))
                {
                    $url .= '&bill='. $this->request->get['bill']; 
                }
                if(isset($this->request->get['date_bill']))
                {
                    $url .= '&date_bill=' . $this->request->get['date_bill'];
                }
		if (isset($this->request->get['id'])) {
			$url .= '&id=' . urlencode($this->request->get['id']);
		}               
                if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
                
                $this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       	        'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token']),
       		'text'      => $this->language->get('text_home'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('Mart_Crm/Crm_customer/bills', 'token=' . $this->session->data['token'] . $url),
       		'text'      => $this->language->get('heading_title1'),
      		'separator' => ' :: '
   		);
                $data = array(
                   'id'   => $id,
                    'bill' => $bill,
                    'date_bill' => $date_bill,
                   'start'           => ($page - 1) * $this->config->get('config_admin_limit'),
                   'limit'           => $this->config->get('config_admin_limit')
                );
                $results = $this->model_Mart_Crm_Crm_customer->getbills($data);
               // print_r($results);
                if($results!=null)
                {
                    $bills_total = count($results);
                    foreach ($results as $result) {

                        $a = $result['bill_date'];
                        $format_date = date('d-m-Y',  strtotime($a));
			$this->data['bills'][] = array(
                          'bill'       =>  $result['name'],
                          'bill_date' =>  $format_date,
                          'customertamount' => $result['amount'],
                          'point'    => $result['point'],
                          'href'  => $this->url->link('Mart_Crm/Crm_customer/detailedbills', 'token=' . $this->session->data['token'] .'&id=' . $result['id'].'&customerid=' . $id
                                  , 'SSL')
                        );
		}
                }
                else
                {
                    $this->data['bills'][] = null;
                      
                    $bills_total=0;
                    
                }
                $this->data['heading_title1'] = $this->language->get('heading_title1');		
		 $this->data['button_export_excel'] = $this->language->get('button_export_excel');
		$this->data['customerbills'] = $this->language->get('customerbills');
                $this->data['customerdate_enter'] = $this->language->get('customerdate_enter');
                $this->data['customertamount'] = $this->language->get('customertamount');
		$this->data['billsdetailed'] = $this->language->get('customerbills');  
                $this->data['point']  = $this->language->get('point');
                $this->data['token'] = $this->session->data['token'];                
                $this->data['id']    = $id;
                 $this->data['export_excel'] = $this->url->link('tool/export/downloadinvoice', 'token=' . $this->session->data['token'].$url,'SSL');
                $pagination = new Pagination();
                $this->data['bill'] = $bill;
                $this->data['date_bill'] = $date_bills;
		$pagination->page = $page;
                $pagination->total = $bills_total;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('Mart_Crm/Crm_customer/bills', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
                
                $this->data['pagination'] = $pagination->render();
                
		$this->template = 'Mart_Crm/Crm_customer_bills.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
                    'common/infocustomer'
		);
				
		$this->response->setOutput($this->render());
}
private function getdetailedbills(){
     if (isset($this->request->get['id'])) {
		$id = $this->request->get['id'];
		} else {
			$id = NULL;
		} 
//            if (isset($this->request->get['bill'])) {
//		$bill = $this->request->get['bill'];
//		} else {
//			$bill = NULL;
//		}  
                if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
                $url = '';
		if (isset($this->request->get['customerid'])) {
			$cusid= $this->request->get['customerid'];
		}
		if (isset($this->request->get['customerid'])) {
			$url .= '&customerid=' . urlencode($this->request->get['customerid']);
		}
                if (isset($this->request->get['id'])) {
			$url .= '&id=' . urlencode($this->request->get['id']);
		}
                if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
                
                $this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       	        'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token']),
       		'text'      => $this->language->get('text_home'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('Mart_Crm/Crm_customer/detailedbills', 'token=' . $this->session->data['token'] . $url, 'SSL'),
       		'text'      => $this->language->get('heading_title2'),
      		'separator' => ' :: '
   		);  
                $results = $this->model_Mart_Crm_Crm_customer->getdetailedbills($id);
                

                $bills_total = count($results);
                  foreach ($results as $result) {
			$this->data['detailed'][] = array(
                          'name'       =>  $result['name'],
                          'amount'     =>  $result['amount'],
                          'quantity'   =>  $result['quantity'],
                        );
		}
                $link = array(
				'href' => $this->url->link('Mart_Crm/Crm_customer/prints', 'token=' . $this->session->data['token'] . $url, 'SSL')
			);
                $this->data['heading_title2'] = $this->language->get('heading_title2');
		 
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		
		$this->data['name'] = $this->language->get('name');
                $this->data['amount'] = $this->language->get('amount');
                $this->data['quantity'] = $this->language->get('quantity');
                $this->data['link'] = $link;
                $this->data['button_export_excel'] = $this->language->get('button_export_excel');
                $this->data['export_excel'] = $this->url->link('tool/export/downloaddetailebill', 'token=' . $this->session->data['token'].'&id='.$cusid.'&billid='.$id,'SSL');
                $pagination = new Pagination();
		$pagination->page = $page;
                $pagination->total = $bills_total;		
                
                $this->data['pagination'] = $pagination->render();
                
                
		$this->template = 'Mart_Crm/Crm_customer_detailbills.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
                    'common/infocustomer'
		);
				
		$this->response->setOutput($this->render());
}
private function getcustomer(){
    if(isset($this->request->get['id_customer'])){
        $id_customer = $this->request->get['id_customer'];
    } else{
        $id_customer = NULL;
    }
     $url = '';
		
		if (isset($this->request->get['id_customer'])) {
			$url .= '&id_customer=' . urlencode($this->request->get['id_customer']);
		}
        $this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       	        'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token']),
       		'text'      => $this->language->get('text_home'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('Mart_Crm/Crm_customer/customer', 'token=' . $this->session->data['token'] . $url),
       		'text'      => $this->language->get('heading_title4'),
      		'separator' => ' :: '
   		);    
                if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->error['firstname'])) {
			$this->data['error_firstname'] = $this->error['firstname'];
		} else {
			$this->data['error_firstname'] = '';
		}

		if (isset($this->error['lastname'])) {
			$this->data['error_lastname'] = $this->error['lastname'];
		} else {
			$this->data['error_lastname'] = '';
		}
				
		if (isset($this->error['telephone'])) {
			$this->data['error_telephone'] = $this->error['telephone'];
		} else {
			$this->data['error_telephone'] = '';
		}
                if (isset($this->error['address1'])) {
			$this->data['error_address1'] = $this->error['address1'];
		} else {
			$this->data['error_address1'] = '';
		}
                if (isset($this->error['address2'])) {
			$this->data['error_address2'] = $this->error['address2'];
		} else {
			$this->data['error_address2'] = '';
		}
                if (isset($this->error['city'])) {
			$this->data['error_city'] = $this->error['city'];
		} else {
			$this->data['error_city'] = '';
		}
                if (isset($this->error['country'])) {
			$this->data['error_country'] = $this->error['country'];
		} else {
			$this->data['error_country'] = '';
		}
                 if (isset($this->error['fax'])) {
			$this->data['error_fax'] = $this->error['fax'];
		} else {
			$this->data['error_fax'] = '';
		}
            if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
    		unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}  
              $this->data['action'] = $this->url->link('Mart_Crm/Crm_customer/update','token='.$this->session->data['token'].'&id_customer='.$this->request->get['id_customer'], 'SSL');
		if ($this->request->server['REQUEST_METHOD'] != 'POST') {
			$customer = $this->model_Mart_Crm_Crm_customer->getnamecustomer($id_customer);;
		}

		if (isset($this->request->post['firstname'])) {
			$this->data['firstname'] = $this->request->post['firstname'];
		} elseif (isset($customer)) {
			$this->data['firstname'] = $customer[0]['first_name'];
		} else {
			$this->data['firstname'] = '';
		}

		if (isset($this->request->post['lastname'])) {
			$this->data['lastname'] = $this->request->post['lastname'];
		} elseif (isset($customer)) {
			$this->data['lastname'] = $customer[0]['last_name'];
		} else {
			$this->data['lastname'] = '';
		}          
                
                if (isset($this->request->post['email'])) {
			$this->data['email'] = $this->request->post['email'];
		} elseif (isset($customer)) {
			$this->data['email'] = $customer[0]['email_address'];
		} else {
			$this->data['email'] = '';
		}          
		if (isset($this->request->post['telephone'])) {
			$this->data['telephone'] = $this->request->post['telephone'];
		} elseif (isset($customer)) {
			$this->data['telephone'] = $customer[0]['phone_mobile'];
		} else {
			$this->data['telephone'] = '';
		}
                if (isset($this->request->post['address1'])) {
			$this->data['address1'] = $this->request->post['address1'];
		} elseif (isset($customer)) {
			$this->data['address1'] = $customer[0]['alt_address_street'];
		} else {
			$this->data['address1'] = '';
		}
                if (isset($this->request->post['address2'])) {
			$this->data['address2'] = $this->request->post['address2'];
		} elseif (isset($customer)) {
			$this->data['address2'] = $customer[0]['primary_address_street'];
		} else {
			$this->data['address2'] = '';
		}
                if (isset($this->request->post['city'])) {
			$this->data['city'] = $this->request->post['city'];
		} elseif (isset($customer)) {
			$this->data['city'] = $customer[0]['primary_address_city'];
		} else {
			$this->data['city'] = '';
		}
                if (isset($this->request->post['country'])) {
			$this->data['country'] = $this->request->post['country'];
		} elseif (isset($customer)) {
			$this->data['country'] = $customer[0]['primary_address_country'];
		} else {
			$this->data['country'] = '';
		}
                if (isset($this->request->post['fax'])) {
			$this->data['fax'] = $this->request->post['telephone'];
		} elseif (isset($customer)) {
			$this->data['fax'] = $customer[0]['phone_fax'];
		} else {
			$this->data['fax'] = '';
		}

            $this->data['back'] = $this->url->link('Mart_Crm/Crm_customer','token='.$this->session->data['token'],'SSL');
            $pagination = new Pagination();	
            $this->data['heading_title4'] = $this->language->get('heading_title4');
            $this->data['entry_firstname'] = $this->language->get('entry_firstname');
            $this->data['entry_lastname'] = $this->language->get('entry_lastname');
            $this->data['entry_telephone'] = $this->language->get('entry_telephone');
            $this->data['entry_address1'] = $this->language->get('entry_address1');
            $this->data['entry_address2'] = $this->language->get('entry_address2');
            $this->data['entry_city'] = $this->language->get('entry_city');
            $this->data['entry_country'] = $this->language->get('entry_country');
            $this->data['entry_email'] = $this->language->get('entry_email');
            $this->data['entry_fax'] = $this->language->get('entry_fax');
            $this->data['button_continue'] = $this->language->get('button_continue');
            $this->data['button_back'] = $this->language->get('button_back');
            $this->data['pagination'] = $pagination->render();
            $this->template = 'Mart_Crm/customer_information.tpl';
            $this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render()); 
    
}
public function update() {
	 $this->load->language('Mart_Crm/Crm_customer');
         $this->document->setTitle($this->language->get('heading_title'));
         $this->load->model('Mart_Crm/Crm_customer');
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
       		$this->model_Mart_Crm_Crm_customer->editCustomer($this->request->get['id_customer'], $this->request->post);			
			
			$this->session->data['success'] = $this->language->get('text_update');
	  
	  		$this->redirect($this->url->link('Mart_Crm/Crm_customer/customer','token='.$this->session->data['token'].'&id_customer='.$this->request->get['id_customer'], 'SSL'));
    	} 
	  	
		$this->getcustomer();
  	}
private function printbills(){		               
		 if (isset($this->request->get['customerid'])) {
		$customerid = $this->request->get['customerid'];
		} else {
			$customerid = NULL;
		}   
                 if (isset($this->request->get['id'])) {
		$id = $this->request->get['id'];
		} else {
			$id = NULL;
		} 
                		
               
		$name = $this->model_Mart_Crm_Crm_customer->getnamecustomer($customerid);
                $hoten = $name[0]['last_name'].' '.$name[0]['first_name'];
                $results = $this->model_Mart_Crm_Crm_customer->getdetailedbills($id);
                $bills_total = count($results);
                $sdt = $name['0']['phone_mobile'];
                $sum = 0;
                 foreach ($results as $result){
                       $sum = $sum + $result['amount'];
                        }
                  foreach ($results as $result) {
			$this->data['detailed'][] = array(
                          'name'       =>  $result['name'],
                          'amount'     =>  $result['amount'],
                          'quantity'   =>  $result['quantity'],
                        );
		}	
                $time = time();                
                $this->data['hoten'] = $hoten;
                $this->data['time'] = date('d-m-Y',$time);  
                $this->data['tongtien'] = $sum; 
                $this->data['sdt'] = $sdt; 
		$this->template = 'Mart_Crm/print_bills.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render()); 
}
private function validateForm() {
		if ((utf8_strlen($this->request->post['firstname']) < 1) || (utf8_strlen($this->request->post['firstname']) > 32)) {
			$this->error['firstname'] = $this->language->get('error_firstname');
		}

		if ((utf8_strlen($this->request->post['lastname']) < 1) || (utf8_strlen($this->request->post['lastname']) > 32)) {
			$this->error['lastname'] = $this->language->get('error_lastname');
		}		

		if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
			$this->error['telephone'] = $this->language->get('error_telephone');
		}
                if ((utf8_strlen($this->request->post['address1']) < 3) || (utf8_strlen($this->request->post['address1']) > 32)) {
			$this->error['address1'] = $this->language->get('error_address1');
		}
                if ((utf8_strlen($this->request->post['address2']) < 3) || (utf8_strlen($this->request->post['address2']) > 32)) {
			$this->error['address2'] = $this->language->get('error_address2');
		}
                if ((utf8_strlen($this->request->post['city']) < 3) || (utf8_strlen($this->request->post['city']) > 32)) {
			$this->error['city'] = $this->language->get('error_city');
		}
                if ((utf8_strlen($this->request->post['country']) < 3) || (utf8_strlen($this->request->post['country']) > 32)) {
			$this->error['country'] = $this->language->get('error_country');
		}
 if ((utf8_strlen($this->request->post['email']) < 3) || (utf8_strlen($this->request->post['email']) > 32)) {
			$this->error['email'] = $this->language->get('error_country');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
public function autocomplete() {
		$json = array();		
		if (isset($this->request->get['last_name']) || isset($this->request->get['first_name'])|| isset($this->request->get['phonemobile'])||isset($this->request->get['district'])||isset($this->request->get['city'])) {
			$this->load->model('Mart_Crm/Crm_customer');
			
			if (isset($this->request->get['last_name'])) {
				$last_name = $this->request->get['last_name'];
			} else {
				$last_name = '';
			}
                        if (isset($this->request->get['email'])) {
				$email = $this->request->get['email'];
			} else {
				$email = '';
			}
			
			if (isset($this->request->get['first_name'])) {
				$first_name = $this->request->get['first_name'];
			} else {
				$first_name = '';
			}
                        if (isset($this->request->get['district'])) {
				$district = $this->request->get['district'];
			} else {
				$district = '';
			}
                        if (isset($this->request->get['city'])) {
				$city = $this->request->get['city'];
			} else {
				$city = '';
			}
                        if (isset($this->request->get['phonemobile'])) {
				$phonemobile = $this->request->get['phonemobile'];
			} else {
				$phonemobile = '';
			}										
			
			if (isset($this->request->get['limit'])) {
				$limit = $this->request->get['limit'];	
			} else {
				$limit = 20;	
			}					
			$data = array(
				'last_name'         => $last_name,
				'first_name'        => $first_name,	
                                'phonemobile'          => $phonemobile,
                                'dictrict'          => $district,
                                'city'              => $city,
				'start'               => 0,
				'limit'               => $limit,
                            'email'=>$email
			);
			
			$results = $this->model_Mart_Crm_Crm_customer->getcustomer($data);			
			foreach ($results as $result) {	
				$json[] = array(					
					'last_name'       => strip_tags(html_entity_decode($result['last_name'], ENT_QUOTES, 'UTF-8')),	
					'first_name'      => $result['first_name'],
                                        'district'        => $result['primary_address_street'],
                                        'city'            => $result['primary_address_city'],
                                        'phonemobile'     => $result['phone_mobile'],
                                    'email'=>$result['email_address']
				);	
			}
		}

		$this->response->setOutput(json_encode($json));
	}
public function autocomplete_bill() {
		$json = array();		
		if (isset($this->request->get['bill'])||(isset($this->request->get['id'])))
                    {
                    if (isset($this->request->get['id'])) {
				$id = $this->request->get['id'];
			} else {
				$id = '';
			}
                        $this->data['id'] = $id;
			$this->load->model('Mart_Crm/Crm_customer');
			
			if (isset($this->request->get['bill'])) {
				$bill = $this->request->get['bill'];
			} else {
				$bill = '';
			}	
								
			$data = array(
				'bill'         => $bill,
                                'id'           => $id,
			);
			
			$results = $this->model_Mart_Crm_Crm_customer->getbills($data);			
			foreach ($results as $result) {	
				$json[] = array(					
					'bill'       => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),						
				);	
			}
		}

		$this->response->setOutput(json_encode($json));
	}
}
?>
